package br.usp.icmc.biomac.client.view.impl;

import java.util.Comparator;
import java.util.Date;
import java.util.List;

import br.usp.icmc.biomac.client.view.DicomResultCellTable;
import br.usp.icmc.biomac.shared.CSVData;

import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SelectionModel;
import com.google.gwt.view.client.SingleSelectionModel;

public class DicomResultCellTableImpl extends Composite implements DicomResultCellTable {

	private static DicomResultCellTableUiBinder uiBinder = GWT
			.create(DicomResultCellTableUiBinder.class);
	
	Presenter presenter;
	
	@UiField(provided = true)
	CellTable<CSVData> cellTable = new CellTable<CSVData>();
	@UiField
	SimplePager pager1 = new SimplePager();
	@UiField 
	SimplePager pager2 = new SimplePager();
	
	// Create a data provider.
	ListDataProvider<CSVData> dataProvider = new ListDataProvider<CSVData>();
	
	interface DicomResultCellTableUiBinder extends
			UiBinder<Widget, DicomResultCellTableImpl> {
	}
	
	public DicomResultCellTableImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		//cellTable.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
		cellTable.setWidth("100%", true);

		// Connect the table to the data provider.
		dataProvider.addDataDisplay(cellTable);
		
		// Create a Pager to control the table.
		pager1.setDisplay(cellTable);
		pager2.setDisplay(cellTable);

		// Attach a column sort handler to the ListDataProvider to sort the list.
		ListHandler<CSVData> sortHandler = new ListHandler<CSVData>(dataProvider.getList());
		cellTable.addColumnSortHandler(sortHandler);

		// Add a selection model so we can select cells.
		final SingleSelectionModel<CSVData> selectionModel = new SingleSelectionModel<CSVData>();
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				CSVData selected = selectionModel.getSelectedObject();
				if (selected != null) {
					setThubmanais(selected.getStudyUID());
				}
			}
		});
		
		cellTable.setSelectionModel(selectionModel);
		// Initialize the cell table
		initTableColumns(selectionModel, sortHandler);
	}

	@Override
	public Widget asWidget() {
		return this;
	}
	
	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}
	
	@Override
	public void setDataResult(List<CSVData> data) {
		// Add the data to the data provider, which automatically pushes it to
		// the widget.
		List<CSVData> list = dataProvider.getList();
		list.clear();
		for (CSVData csvdata : data) {
			list.add(csvdata);
		}	
	}
	
	public void setThubmanais(String studyUID){
		GWT.log("setThubmanais");
		presenter.loadThubmanais(studyUID);//Window.alert("Buh");
	}

	public void initTableColumns(final SelectionModel<CSVData> selectionModel, ListHandler<CSVData> sortHandler) {

		// Create studyUID column.
		Column<CSVData, String> studyUID = new Column<CSVData, String>(
				new TextCell()) {
			@Override
			public String getValue(CSVData object) {
				return object.getStudyUID();
			}
		};
		// Make the studyUID column sortable.
		studyUID.setSortable(true);
		sortHandler.setComparator(studyUID, new Comparator<CSVData>() {
			@Override
			public int compare(CSVData arg0, CSVData arg1) {
				return arg0.getStudyUID().compareTo(arg1.getStudyUID());
			}
		});
		// add studyUID in cellTable
		cellTable.addColumn(studyUID, "Study UID");
		cellTable.setColumnWidth(studyUID, 250, Unit.PX);

		// Create patientName column.
		Column<CSVData, String> patientName = new Column<CSVData, String>(
				new TextCell()) {
			@Override
			public String getValue(CSVData data) {
				return data.getPatientName();
			}
		};
		// Make the patientName column sortable.
		patientName.setSortable(true);
		sortHandler.setComparator(patientName, new Comparator<CSVData>() {
			@Override
			public int compare(CSVData arg0, CSVData arg1) {
				return arg0.getPatientName().compareTo(arg1.getPatientName());
			}
		});
		// add patientID in cellTable
		cellTable.addColumn(patientName, "Patient Name");
		cellTable.setColumnWidth(patientName, 70, Unit.PX);

		// Create patientID column.
		Column<CSVData, String> patientID = new Column<CSVData, String>(
				new TextCell()) {
			@Override
			public String getValue(CSVData data) {
				return data.getPatientID();
			}
		};
		// Make the patientID column sortable.
		patientID.setSortable(true);
		sortHandler.setComparator(patientID, new Comparator<CSVData>() {
			@Override
			public int compare(CSVData o1, CSVData o2) {
				return o1.getPatientID().compareTo(o2.getPatientID());
			}
		});
		// add patientID in cellTable
		cellTable.addColumn(patientID, "Patient ID");
		cellTable.setColumnWidth(patientID, 75, Unit.PX);

		// Create examType column.
		Column<CSVData, String> examType = new Column<CSVData, String>(
				new TextCell()) {
			@Override
			public String getValue(CSVData data) {
				return data.getExamType();
			}
		};
		// Make the examType column sortable.
		examType.setSortable(true);
		sortHandler.setComparator(examType, new Comparator<CSVData>() {
			@Override
			public int compare(CSVData o1, CSVData o2) {
				return o1.getExamType().compareTo(o2.getExamType());
			}
		});
		// add examType in cellTable
		cellTable.addColumn(examType, "Exam Type");
		cellTable.setColumnWidth(examType, 75, Unit.PX);

		// Create patientID column.
		Column<CSVData, Date> dateAcquired = new Column<CSVData, Date>(
				new DateCell()) {
			@Override
			public Date getValue(CSVData object) {
				return object.getDateAcquired();
			}
		};
		// Make the examType column sortable.
		dateAcquired.setSortable(true);
		sortHandler.setComparator(dateAcquired, new Comparator<CSVData>() {
			@Override
			public int compare(CSVData o1, CSVData o2) {
				return o1.getDateAcquired().compareTo(o2.getDateAcquired());
			}
		});
		// add dateAcquired in cellTable
		cellTable.addColumn(dateAcquired, "Date Acquired");
		cellTable.setColumnWidth(dateAcquired, 150, Unit.PX);

	}
}